#include "stdafx.h"
#include "ShopDef.h"
#include "InstanceTouch.h"
#include <sstream>
#include <algorithm>

DECL_INSTANCE_TOUCH(ETouch_ShopDef);

struct  ShopReflectionRegister
{
	ShopReflectionRegister()
	{
		REGISTER_REFLECTION_MEMBER(ShopFunction,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(ShopFunction,m_sDisplayName,std::string("DefaultDisplayName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(ShopFunction,m_eShopFunc,EShopFunc_BuyCard).Editor_SetAsEnumation<EShopFunc>();
		REGISTER_REFLECTION_MEMBER(ShopFunction,m_eShopMoney,EShopMoney_Gold).Editor_SetAsEnumation<EShopMoney>();
		//REGISTER_REFLECTION_MEMBER(ShopFunction,m_sName,std::string("DefaultName")).Editor_SetAsRaw();
		//REGISTER_REFLECTION_MEMBER(ShopFunction,m_sDesc,std::string("DefaultDesc")).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(ShopFunction,m_sButtonText,std::string("Buy")).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(ShopFunction,m_iCost,1).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(ShopFunction,m_iDropTable,INVALID_ID).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(ShopFunction,m_eCounter,eDropCounter0).Editor_SetAsEnumation<EDropCounter>();
		REGISTER_REFLECTION_MEMBER(ShopFunction,m_iDropItemCount,1).Editor_SetAsRaw();


		REGISTER_REFLECTION_MEMBER(ShopItem,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(ShopItem,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(ShopItem,m_sName,std::string("DefaultName")).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(ShopItem,m_sIcon,std::string("m_sIcon")).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(ShopItem,m_sDesc,std::string("m_sDesc")).Editor_SetAsRaw();

		REGISTER_REFLECTION_MEMBER(ShopItem,m_bActive,true).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(ShopItem,m_iOrder,0).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(ShopItem,m_vFunctionIDs,std::vector<int32>()).Editor_SetAsForeignKeySet(eDefGroup_Default,eDefaultDef_ShopFunction);
		REGISTER_REFLECTION_MEMBER(ShopItem,m_sFromDate,std::string("2013-01-01")).Editor_SetAsDateTime("Time");
		REGISTER_REFLECTION_MEMBER(ShopItem,m_iFromHour,12).Editor_SetAsRaw("Time");
		REGISTER_REFLECTION_MEMBER(ShopItem,m_sToDate,std::string("2013-01-02")).Editor_SetAsDateTime("Time");
		REGISTER_REFLECTION_MEMBER(ShopItem,m_iToHour,12).Editor_SetAsRaw("Time");
		REGISTER_REFLECTION_MEMBER(ShopItem,m_ePerWeekDay,EPerWeekDay_EveryDay).Editor_SetAsEnumation<EPerWeekDay>("Time");
		REGISTER_REFLECTION_MEMBER(ShopItem,m_iFromTimeOfDay,0).Editor_SetAsRaw("Time");
		REGISTER_REFLECTION_MEMBER(ShopItem,m_iToTimeOfDay,0).Editor_SetAsRaw("Time");
		REGISTER_REFLECTION_MEMBER(ShopItem,m_eShopFlag,eShopFlag_None).Editor_SetAsEnumation<EShopFlag>();


	

		GlobalClassManager::Instance().RegisterClass(ShopItem::s_GetReflection(),eDefaultDef_ShopItem);
		GlobalDefManager::Instance().RegisterDefManger(&ShopItemManager::Instance());

		GlobalClassManager::Instance().RegisterClass(ShopFunction::s_GetReflection(),eDefaultDef_ShopFunction);
		GlobalDefManager::Instance().RegisterDefManger(&ShopFunctionManager::Instance());

	}
}____shopreflectionregister;

static bool __CompareFuncOrder(const  ShopFunction* pA,const  ShopFunction* pB)
{
	return pA->m_iCost<pB->m_iCost;
}
rewrite  void ShopItem::OnArchieveReaded()
{
	m_vShoFunctions.clear();
	for(uint32 ii=0;ii<m_vFunctionIDs.size();ii++)
	{
		const  ShopFunction* pFunc=ShopFunctionManager::Instance().LookupItem(m_vFunctionIDs[ii]);
		if(NULL==pFunc) continue;
		m_vShoFunctions.push_back(pFunc);

	}
	std::sort(m_vShoFunctions.begin(),m_vShoFunctions.end(),__CompareFuncOrder);

	{
		int32 iHour=m_iFromHour;
		if(iHour<0 ||iHour>23) iHour=12;
		std::stringstream ss;
		ss<<m_sFromDate<<" "<<iHour<<":00:00";
		m_iFromDateTime=Util::PraseISOTimeString(ss.str().c_str());
	}
	{
		int32 iHour=m_iToHour;
		if(iHour<0 ||iHour>23) iHour=12;
		std::stringstream ss;
		ss<<m_sToDate<<" "<<iHour<<":00:00";
		m_iToDateTime=Util::PraseISOTimeString(ss.str().c_str());
	}

}

static bool CompareShopItemOrder(const ShopItem* pA,const ShopItem* pB)
{
	return pA->m_iOrder<pB->m_iOrder;
}
void ShopItemManager::OnArchieveReaded()
{
	__ShopItemManager::OnArchieveReaded();

	m_vShopMenus.clear();
	ShopItemManager::Instance().Enumerate(m_vShopMenus);
	std::sort(m_vShopMenus.begin(),m_vShopMenus.end(),CompareShopItemOrder);
}


